/////////////////////////////////////////////////////////////////////////////
// Name:        tts_property.cpp
// Purpose:     Text to speech property classes
// Author:      Julian Smart
// Modified by:
// Created:     2009-02-14
// RCS-ID:      $Id$
// Copyright:   (c) Julian Smart
// Licence:     New BSD License
/////////////////////////////////////////////////////////////////////////////

#include "wx/wx.h"

#include "tts/tts_property.h"

IMPLEMENT_CLASS(wxTTSProperty, wxObject)

wxString wxTTSPropertyCategory::sm_category;

IMPLEMENT_CLASS(wxTTSProperties, wxObject)

wxTTSProperty* wxTTSProperties::AddProperty(wxTTSProperty* property, const wxString& insertAfter)
{
    wxTTSProperty* oldProp = FindProperty(property->GetName());
    if (oldProp)
        m_list.DeleteObject(oldProp);

    wxObjectList::compatibility_iterator insertBeforeNode = NULL;
    if (!insertAfter.IsEmpty())
    {
        wxTTSProperty* insertAfterProp = FindProperty(insertAfter);
        if (insertAfterProp)
        {
            wxObjectList::compatibility_iterator node = m_list.Member(insertAfterProp);
            if (node)
                insertBeforeNode = node->GetNext();
        }
    }
    if (insertBeforeNode)
        m_list.Insert(insertBeforeNode, property);
    else
        m_list.Append(property);

    if (oldProp)
    {
        property->GetVariant() = oldProp->GetVariant();

        delete oldProp;
    }
    return property;
}

void wxTTSProperties::RemoveProperty(wxTTSProperty* property)
{
    m_list.DeleteObject(property);
}

void wxTTSProperties::DeleteProperty(wxTTSProperty* property)
{
    m_list.DeleteObject(property);
    delete property;
}

void wxTTSProperties::DeleteProperty(size_t i)
{
    wxTTSProperty* prop = GetProperty(i);
    if (prop)
        DeleteProperty(prop);
}

void wxTTSProperties::DeleteProperty(const wxString& name)
{
    wxTTSProperty* prop = FindProperty(name);
    if (prop)
    {
        m_list.DeleteObject(prop);
        delete prop;
    }
}

wxTTSProperty* wxTTSProperties::FindProperty(const wxString& name) const
{
    wxObjectList::compatibility_iterator node = m_list.GetFirst();
    while (node)
    {
        wxTTSProperty* prop = (wxTTSProperty*) node->GetData();
        if (prop->GetName() == name)
            return prop;

        node = node->GetNext();
    }
    return (wxTTSProperty*) NULL;
}

wxTTSProperty* wxTTSProperties::FindPropertyByDisplayName(const wxString& name)
{
    wxObjectList::compatibility_iterator node = m_list.GetFirst();
    while (node)
    {
        wxTTSProperty* prop = (wxTTSProperty*) node->GetData();
        if (prop->GetDisplayName() == name)
            return prop;

        node = node->GetNext();
    }
    return (wxTTSProperty*) NULL;
}

wxVariant wxTTSProperties::FindPropertyValue(const wxString& name) const
{
    wxTTSProperty* prop = FindProperty(name);
    if (prop)
        return prop->m_variant;
    else
        return wxEmptyString;
}

wxString wxTTSProperties::FindPropertyValueString(const wxString& name) const
{
    wxTTSProperty* prop = FindProperty(name);
    if (prop)
        return prop->m_variant.GetString();
    else
        return wxEmptyString;
}

bool wxTTSProperties::FindPropertyValueBool(const wxString& name) const
{
    wxTTSProperty* prop = FindProperty(name);
    if (prop)
        return prop->m_variant.GetBool();
    else
        return false;
}

long wxTTSProperties::FindPropertyValueLong(const wxString& name) const
{
    wxTTSProperty* prop = FindProperty(name);
    if (prop)
        return prop->m_variant.GetLong();
    else
        return 0;
}

double wxTTSProperties::FindPropertyValueDouble(const wxString& name) const
{
    wxTTSProperty* prop = FindProperty(name);
    if (prop)
        return prop->m_variant.GetDouble();
    else
        return 0.0;
}

wxTTSProperty* wxTTSProperties::FindOrCreateProperty(const wxString& name)
{
    wxTTSProperty* prop = FindProperty(name);
    if (!prop)
    {
        prop = new wxTTSProperty(name);
        AddProperty(prop);
    }
    return prop;
}

void wxTTSProperties::Clear()
{
    m_list.DeleteContents(TRUE);
    m_list.Clear();
    m_list.DeleteContents(FALSE);
}

void wxTTSProperties::Copy(const wxTTSProperties& properties)
{
    wxObjectList::compatibility_iterator node = properties.m_list.GetFirst();
    while (node)
    {
        wxTTSProperty* prop = (wxTTSProperty*) node->GetData();

        AddProperty(new wxTTSProperty(* prop));

        node = node->GetNext();
    }
}

bool wxTTSProperties::operator == (const wxTTSProperties& properties)
{
    if (m_list.GetCount() != properties.m_list.GetCount())
        return false;

    wxObjectList::compatibility_iterator node1 = m_list.GetFirst();
    wxObjectList::compatibility_iterator node2 = properties.m_list.GetFirst();
    while (node1 && node2)
    {
        wxTTSProperty* prop1 = (wxTTSProperty*) node1->GetData();
        wxTTSProperty* prop2 = (wxTTSProperty*) node2->GetData();

        if (!(*prop1 == *prop2))
            return false;

        node1 = node1->GetNext();
        node2 = node2->GetNext();
    }
    return true;
}

void wxTTSProperties::SetProperty(const wxTTSProperty& prop)
{
    wxTTSProperty* p = FindOrCreateProperty(prop.GetName());
    (*p) = prop;
}

void wxTTSProperties::SetProperty(const wxString& name, const wxString& value)
{
    wxTTSProperty* prop = FindOrCreateProperty(name);
    prop->SetValue(value);
}

void wxTTSProperties::SetProperty(const wxString& name, long value)
{
    wxTTSProperty* prop = FindOrCreateProperty(name);
    prop->GetVariant() = value;
}

void wxTTSProperties::SetProperty(const wxString& name, double value)
{
    wxTTSProperty* prop = FindOrCreateProperty(name);
    prop->GetVariant() = value;
}

void wxTTSProperties::SetProperty(const wxString& name, bool value)
{
    wxTTSProperty* prop = FindOrCreateProperty(name);
    prop->GetVariant() = (bool) value;
}

void wxTTSProperties::SetProperty(const wxString& name, const wxVariant& value)
{
    wxTTSProperty* prop = FindOrCreateProperty(name);
    prop->SetValue(value);
}

wxTTSProperty* wxTTSProperties::GetNth(int i) const
{
    wxASSERT( i < (int) GetCount() );
    if (i < (int) GetCount())
    {
        wxObjectList::compatibility_iterator node = m_list.Item(i);
        return (wxTTSProperty*) node->GetData();
    }
    return NULL;
}
